'use strict'
const cnblogs = require('../models/cnblog');
var i = 1;
var Crawler = require("crawler");

module.exports = () => {
    var c = new Crawler({
        rateLimit: 10,
        maxConnections: 1,
        // This will be called for each crawled page
        callback: function (error, res, done) {
            if (error) {
                console.log(error);
            } else {
                var $ = res.$;
                var list = $('.dw_table .el').toArray();
                list.forEach(element => {
                    var job = $(element).find('.t1 a').attr('title');
    
                    var company = $(element).find('.t2 a').text();
    
                    var sp = $(element).find('.t3').text();
    
                    var money = $(element).find('.t4').text();
    
                    var newtime = $(element).find('.t5').text();
    
                    table.create({ Job: job, company:company, sp:sp, money:money,newtime:newtime })
                });
                // $ is Cheerio by default
                //a lean implementation of core jQuery designed specifically for the server
    
                done();
    
            }
        }
    });
    
    // Queue just one URL, with default callback
    for (var index = 1; index <= 3; index++) {
        var URL = 'https://search.51job.com/list/110300,000000,0000,32,9,99,%25E8%25BD%25AF%25E4%25BB%25B6%25E5%25B7%25A5%25E7%25A8%258B%25E5%25B8%2588,2,' + index + '.html?lang=c&postchannel=0000&workyear=99&cotype=99&degreefrom=99&jobterm=99&companysize=99&ord_field=0&dibiaoid=0&line=&welfare=='
        c.queue(URL);
    }
}